// mircoApp/numberCollection/comm/sponPopup/sponPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  options: {  //小程序获取外部样式
    addGlobalClass: true,
  },
  properties: {
    sponShow: Boolean,
    maxtotal:Number,
    params:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    memberFlag:false,
    memberName:{},
    memberId:'',
    maxSum:1,
    timeoutId:''
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
      
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    sponClose() {
      this.setData({
        sponShow:false,
      })
    },
    sponsorClick() {
      if(this.data.memberFlag && this.data.memberId) {
        // this.setData({
        //   sponShow:false,
        // })
        this.triggerEvent('sponsorConfrim', {memberId:this.data.memberId,maxSum:this.data.maxSum});
      } else {
        wx.showToast({
          icon: "none",
          title: "请输入正确会员ID",
          duration: 1000,
        });
      }
      
    },
    onSumChange(event) {
      this.setData({
        maxSum:event.detail
      })
    },
    throttleFn(event) {
      if (this.data.timeoutId) {
        clearTimeout(this.data.timeoutId);
      }
      this.data.timeoutId = setTimeout(() => {
        // this.data.memberId = this.data.params.transfer_id;
        this.setData({
          memberId:event.detail.value
        })
          this.searchMember()
      }, 300);
    },
    searchMember() { //会员信息
      let urlStr = app.getNetAddresss("plugin.collection-center.frontend.controllers.collection.member.search-member");
      app._postNetWork({
        url: urlStr,
        data: {member_id: Number(this.data.memberId)},
        success: (resdata) => {
          var res = resdata.data;
          let data = res.data;
          if (res.result == 1) {
            let memberName = data.member?.nickname;
            memberName?this.data.memberFlag = true:this.data.memberFlag = false;
            this.setData({
              memberName:memberName,
              memberFlag:this.data.memberFlag
            })
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
  }
})
